** Organize data in a format that is ready for analysis of oldest generation in 
** empty-nester households
*******************************************************************************


*************************
** 1990
*************************

***********
** Wave 3
***********

use EmptyNesterGPData1990_w3

** Merge in well-being variables
merge m:1 suid entry pnum using WellBeingVars1990_w3 
drop if _merge==2
drop _merge

** Merge in previously-constructed info about EITC eligibility for 
** "middle" generation living in same household

merge m:1 suid entry srefmon using MaxMidGenEmptyNestEITC1990_w3
drop if _merge==2
drop _merge
merge m:1 suid entry srefmon using SumMidGenEmptyNestEITC_1990_w3
drop if _merge==2
drop _merge

recode panel .=1990
save OrganizedOldGenEmptyNestData1990_w3, replace



***********
** Wave 4
***********

use EmptyNesterGPData1990_w4

** Merge in well-being variables
merge m:1 suid entry pnum using WellBeingVars1990_w4 
drop if _merge==2
drop _merge

** Merge in previously-constructed info about EITC eligibility for 
** "middle" generation living in same household

merge m:1 suid entry srefmon using MaxMidGenEmptyNestEITC1990_w4
drop if _merge==2
drop _merge
merge m:1 suid entry srefmon using SumMidGenEmptyNestEITC_1990_w4
drop if _merge==2
drop _merge

recode panel .=1990
save OrganizedOldGenEmptyNestData1990_w4, replace



*************************
** 1991
*************************

***********
** Wave 3
***********

use EmptyNesterGPData1991_w3

** Merge in well-being variables
merge m:1 suid entry pnum using WellBeingVars1991_w3 
drop if _merge==2
drop _merge

** Merge in previously-constructed info about EITC eligibility for 
** "middle" generation living in same household

merge m:1 suid entry srefmon using MaxMidGenEmptyNestEITC1991_w3
drop if _merge==2
drop _merge
merge m:1 suid entry srefmon using SumMidGenEmptyNestEITC_1991_w3
drop if _merge==2
drop _merge

recode panel .=1991
save OrganizedOldGenEmptyNestData1991_w3, replace



***********
** Wave 7
***********

use EmptyNesterGPData1991_w7

** Merge in well-being variables
merge m:1 suid entry pnum using WellBeingVars1991_w7 
drop if _merge==2
drop _merge

** Merge in previously-constructed info about EITC eligibility for 
** "middle" generation living in same household

merge m:1 suid entry srefmon using MaxMidGenEmptyNestEITC1991_w7
drop if _merge==2
drop _merge
merge m:1 suid entry srefmon using SumMidGenEmptyNestEITC_1991_w7
drop if _merge==2
drop _merge

recode panel .=1991
save OrganizedOldGenEmptyNestData1991_w7, replace



*************************
** 1992
*************************

***********
** Wave 4
***********

use EmptyNesterGPData1992_w4

** Merge in well-being variables
merge m:1 suid entry pnum using WellBeingVars1992_w4 
drop if _merge==2
drop _merge

** Merge in previously-constructed info about EITC eligibility for 
** "middle" generation living in same household

merge m:1 suid entry srefmon using MaxMidGenEmptyNestEITC1992_w4
drop if _merge==2
drop _merge
merge m:1 suid entry srefmon using SumMidGenEmptyNestEITC_1992_w4
drop if _merge==2
drop _merge

recode panel .=1992
save OrganizedOldGenEmptyNestData1992_w4, replace



***********
** Wave 6
***********

use EmptyNesterGPData1992_w6

** Merge in well-being variables
merge m:1 suid entry pnum using WellBeingVars1992_w6 
drop if _merge==2
drop _merge

** Merge in previously-constructed info about EITC eligibility for 
** "middle" generation living in same household

merge m:1 suid entry srefmon using MaxMidGenEmptyNestEITC1992_w6
drop if _merge==2
drop _merge
merge m:1 suid entry srefmon using SumMidGenEmptyNestEITC_1992_w6
drop if _merge==2
drop _merge

recode panel .=1992
save OrganizedOldGenEmptyNestData1992_w6, replace



*************************
** 1993
*************************

***********
** Wave 3
***********

use EmptyNesterGPData1993_w3

** Merge in well-being variables
merge m:1 suid entry pnum using WellBeingVars1993_w3 
drop if _merge==2
drop _merge

** Merge in previously-constructed info about EITC eligibility for 
** "middle" generation living in same household

merge m:1 suid entry srefmon using MaxMidGenEmptyNestEITC1993_w3
drop if _merge==2
drop _merge
merge m:1 suid entry srefmon using SumMidGenEmptyNestEITC_1993_w3
drop if _merge==2
drop _merge

recode panel .=1993
save OrganizedOldGenEmptyNestData1993_w3, replace


***********
** Wave 7
***********

use EmptyNesterGPData1993_w7

** Merge in well-being variables
merge m:1 suid entry pnum using WellBeingVars1993_w7 
drop if _merge==2
drop _merge

** Merge in previously-constructed info about EITC eligibility for 
** "middle" generation living in same household

merge m:1 suid entry srefmon using MaxMidGenEmptyNestEITC1993_w7
drop if _merge==2
drop _merge
merge m:1 suid entry srefmon using SumMidGenEmptyNestEITC_1993_w7
drop if _merge==2
drop _merge

recode panel .=1993
save OrganizedOldGenEmptyNestData1993_w7, replace



*************************
** 1996
*************************

***********
** Wave 3
***********

use EmptyNesterGPData1996_w3

** Merge in well-being variables
merge m:1 ssuid epppnum using WellBeingVars1996_w3 
drop if _merge==2
drop _merge

** Merge in info about EITC eligibility for middle generation living in same househod
merge m:1 ssuid srefmon using MaxMidGenEmptyNestEITC1996_w3
drop if _merge==2
drop _merge
merge m:1 ssuid srefmon using SumMidGenEmptyNestEITC_1996_w3
drop if _merge==2
drop _merge

recode panel .=1996
save OrganizedOldGenEmptyNestData1996_w3, replace





*************************
** 2001
*************************

***********
** Wave 3
***********

use EmptyNesterGPData2001_w3

** Merge in well-being variables
merge m:1 ssuid epppnum using WellBeingVars2001_w3 
drop if _merge==2
drop _merge

** Merge in info about EITC eligibility for middle generation living in same househod
merge m:1 ssuid srefmon using MaxMidGenEmptyNestEITC2001_w3
drop if _merge==2
drop _merge
merge m:1 ssuid srefmon using SumMidGenEmptyNestEITC_2001_w3
drop if _merge==2
drop _merge

recode panel .=2001
save OrganizedOldGenEmptyNestData2001_w3, replace


*************************
** 2004
*************************

***********
** Wave 3
***********

use EmptyNesterGPData2004_w3

** Merge in well-being variables
merge m:1 ssuid epppnum using WellBeingVars2004_w3 
drop if _merge==2
drop _merge

** Merge in info about EITC eligibility for middle generation living in same househod
merge m:1 ssuid srefmon using MaxMidGenEmptyNestEITC2004_w3
drop if _merge==2
drop _merge
merge m:1 ssuid srefmon using SumMidGenEmptyNestEITC_2004_w3
drop if _merge==2
drop _merge

recode panel .=2004
save OrganizedOldGenEmptyNestData2004_w3, replace




*************************
** 2008
*************************

***********
** Wave 4
***********

use EmptyNesterGPData2008_w4

** Merge in well-being variables
merge m:1 ssuid epppnum using WellBeingVars2008_w4
drop if _merge==2
drop _merge

** Merge in info about EITC eligibility for middle generation living in same househod
merge m:1 ssuid srefmon using MaxMidGenEmptyNestEITC2008_w4
drop if _merge==2
drop _merge
merge m:1 ssuid srefmon using SumMidGenEmptyNestEITC_2008_w4
drop if _merge==2
drop _merge

recode panel .=2008
save OrganizedOldGenEmptyNestData2008_w4, replace





*************************
** 2014
*************************

***********
** Wave 1
***********

use EmptyNesterGPData2014_w1

** Merge in well-being variables
merge m:1 ssuid pnum monthcode using WellBeingVars2014_w1
drop if _merge==2
drop _merge

** Merge in info about EITC eligibility for middle generation living in same househod
merge m:1 ssuid monthcode using MaxMidGenEmptyNestEITC2014_w1
drop if _merge==2
drop _merge
merge m:1 ssuid monthcode using SumMidGenEmptyNestEITC_2014_w1
drop if _merge==2
drop _merge

recode panel .=2014
save OrganizedOldGenEmptyNestData2014_w1, replace


***********************************************************
***********************************************************

use OrganizedOldGenEmptyNestData1990_w3
append using OrganizedOldGenEmptyNestData1990_w4
append using OrganizedOldGenEmptyNestData1991_w3
append using OrganizedOldGenEmptyNestData1991_w7
append using OrganizedOldGenEmptyNestData1992_w4
append using OrganizedOldGenEmptyNestData1992_w6
append using OrganizedOldGenEmptyNestData1993_w3
append using OrganizedOldGenEmptyNestData1993_w7
append using OrganizedOldGenEmptyNestData1996_w3
append using OrganizedOldGenEmptyNestData2001_w3
append using OrganizedOldGenEmptyNestData2004_w3
append using OrganizedOldGenEmptyNestData2008_w4

append using OrganizedOldGenEmptyNestData2014_w1

recode rhcalyr 90=1990
recode rhcalyr 91=1991
recode rhcalyr 92=1992
recode rhcalyr 93=1993
recode rhcalyr 94=1994
recode rhcalyr 95=1995
replace rhcalyr = 2013 if panel==2014 & swave==1


** Should drop cases in which tfipsst code does not uniquely identify state, when the 
** group of states includes some with different EITC parameters
drop if (panel==1990 | panel==1991 | panel==1992 | panel==1993) & (tfipsst==61 | tfipsst==62 | tfipsst==63)
drop if panel==1996 & (tfipsst==61 | tfipsst==62)
drop if panel==2001 & (tfipsst==61 | tfipsst==62)

** For 2014, drop any cases with state identifier indicating Puerto Rico or foreign country
drop if panel==2014 & (tfipsst==60 | tfipsst==61)


** Impose a (sensible but arbitrary) minimum age restriction
drop if tage<30


** Person identifer

egen UniquePersonID_p2014 = group(panel ssuid pnum)
replace UniquePersonID_p2014 = . if panel~=2014

egen UniquePersonID_midyears = group(panel ssuid epppnum )
replace UniquePersonID_midyears = . if (panel==1990 | panel==1991 | panel==1992 | panel==1993 | panel==2014)

egen UniquePersonID_p1993 = group(panel suid entry pnum)
replace UniquePersonID_p1993 = . if (panel==1996 | panel==2001 | panel==2004 | panel==2008 | panel==2014)

gen temp_UniquePersonID = UniquePersonID_midyears
replace temp_UniquePersonID = UniquePersonID_p1993 if (panel==1990 | panel==1991 | panel==1992 | panel==1993)
replace temp_UniquePersonID = UniquePersonID_p2014 if panel==2014

egen GP_UniquePersonID = group(panel temp_UniquePersonID)


** Household identifier

egen later_UniqueHouseholdID = group(panel ssuid )
replace later_UniqueHouseholdID = . if (panel==1990 | panel==1991 | panel==1992 | panel==1993)

egen earlier_UniqueHouseholdID = group(panel suid entry)
replace earlier_UniqueHouseholdID = . if (panel==1996 | panel==2001 | panel==2004 | panel==2008 | panel==2014)

gen temp_UniqueHouseholdID = later_UniqueHouseholdID
replace temp_UniqueHouseholdID = earlier_UniqueHouseholdID if (panel==1990 | panel==1991 | panel==1992 | panel==1993)

egen GP_UniqueHouseholdID = group(panel temp_UniqueHouseholdID)




** Month-by-month variation is not meaningful for the well-being variables that are asked about longer reference period
** Trim down to one obs per person per swave
duplicates report GP_UniquePersonID
gen uniform_srefmon = srefmon
replace uniform_srefmon = monthcode if panel==2014
sort GP_UniquePersonID swave uniform_srefmon
by GP_UniquePersonID swave: egen min_srefmon = min(uniform_srefmon)
keep if uniform_srefmon==min_srefmon
drop min_srefmon
duplicates report GP_UniquePersonID
duplicates report GP_UniquePersonID swave


gen ExcellentVeryGoodHealth = (ehltstat==1 | ehltstat==2)
replace ExcellentVeryGoodHealth = . if ehltstat==0
replace ExcellentVeryGoodHealth = . if ehltstat==.

gen AgeSq = tage*tage

gen RefPerson=(errp==1 | errp==2)
replace RefPerson = 1 if ((erelrp==1 | erelrp==2) & panel==2014)

** With this sample of only old-gen parents in memory, count up if any of the 
** old gen sample members are the reference person, and create an indicator
** for being in a household where the ref person is in the old generation
sort GP_UniqueHouseholdID swave uniform_srefmon
by GP_UniqueHouseholdID swave uniform_srefmon: egen N_OldGenRefPerson=total(RefPerson)
gen OldGenHasRefPerson=(N_OldGenRefPerson>0)



gen real = 1
replace real = 229.594/236.736 if rhcalyr==2014
replace real = 229.594/232.957 if rhcalyr==2013

replace real = 229.594/229.594 if rhcalyr==2012
replace real = 229.594/224.939 if rhcalyr==2011
replace real = 229.594/218.056 if rhcalyr==2010
replace real = 229.594/214.537 if rhcalyr==2009
replace real = 229.594/215.303 if rhcalyr==2008
replace real = 229.594/207.342 if rhcalyr==2007
replace real = 229.594/201.6 if rhcalyr==2006
replace real = 229.594/195.3 if rhcalyr==2005
replace real = 229.594/188.9 if rhcalyr==2004
replace real = 229.594/184 if rhcalyr==2003
replace real = 229.594/179.9 if rhcalyr==2002
replace real = 229.594/177.1 if rhcalyr==2001
replace real = 229.594/172.2 if rhcalyr==2000
replace real = 229.594/166.6 if rhcalyr==1999
replace real = 229.594/163 if rhcalyr==1998
replace real = 229.594/160.5 if rhcalyr==1997
replace real = 229.594/156.9 if rhcalyr==1996
replace real = 229.594/152.4 if rhcalyr==1995
replace real = 229.594/148.2 if rhcalyr==1994
replace real = 229.594/144.5 if rhcalyr==1993
replace real = 229.594/140.3 if rhcalyr==1992
replace real = 229.594/136.2 if rhcalyr==1991
replace real = 229.594/130.7 if rhcalyr==1990


gen r_DebtAmount = DebtAmount*real
gen ln_r_Debt = ln(1+r_DebtAmount)

gen r_thtotinc = thtotinc*real

gen r_AssetAmount = AssetAmount*real
gen r_CheckingAmount = CheckingAmount*real
gen r_SavingsCheckingA = SavingsCheckingAmount*real

gen ln_r_AssetAmount = ln(1+r_AssetAmount)
gen ln_r_CheckingAmount=ln(1+r_CheckingAmount)
gen ln_r_SavingsChecking = ln(1+r_SavingsCheckingA)





** Create variables that will be helpful in identifying samples for health-related
** regressions and samples for debt-related regressions
gen MissingHealthInfo=(ehltstat==. | ehltstat==0)
gen MissingDebtInfo = (AnyDebt==. | AnyCCDebt==. | ln_r_Debt==. | AnyChecking==. | ln_r_CheckingAmount==.)



** Generate dummy for receiving (own) Social Security income
** Measured with different variables in 1993 panel and in post-93 panels, although
** you have renamed the 1993 socsec variable essself
gen GetOwnSS = (essself==1)



** For some analysis, it will be useful to identify earliest observation of a given individual
sort GP_UniquePersonID swave
by GP_UniquePersonID: egen min_swave=min(swave)


** Add in info about state-level unemployment, top tax rate, gdp, etc
merge m:1 tfipsst rhcalyr using OrganizedStateControls
tab _merge
keep if _merge==3
drop _merge

gen r_MaxWelf = MaxWelf*real


** You have two ways of aggregating EITC amounts across mid-gen parents in order
** to assign to the old-gen grandparents.  Method #1 is taking the max from the
** mid gen, and Method #2 is summing over all mid-gen mothers, making the assumption
** that each mid-gen mom is in a different tax filing unit. Method #2 results in 
** missing values of the Sum_MaxEITC_MidGenMoms variable whenever there is just a dad
** in the middle generation, no moms. So try a version where you replace missing values
** of Sum_MaxEITC_MidGenMoms with r_MaxEITC_MidGenMax.
gen EITC_v3 = Sum_MaxEITC_MidGenFemales
replace EITC_v3 = r_MaxEITC_MidGenMax if EITC_v3==.

gen ref_EITC_v3 = Sum_ref_MaxEITC_MidGenFemales
replace ref_EITC_v3 = r_ref_MaxEITC_MidGenMax if ref_EITC_v3==.

gen Dep_Control = N_Potential_EN_Deps
replace Dep_Control = Max_FH_N_Dep if Dep_Control==.



** Create a dummy equal to one for those who have all the info needed to appear in health regressions
gen InHealthReg =(ehltst~=. & EITC_v3~=. & tage~=. & AgeSq~=. & Female~=. & Married~=. & White~=. & HSGrad~=. & SomeCollege~=. & CollegeGrad~=. &  unemp_rate~=. & r_MaxWelf~=. & state_rate_wages~=. & minwage~=. & gdp~=. & any_w_a_extended~=. & rhcalyr~=. & tfipsst~=.  & MissingHealthInfo==0)

** Create a dummy equal to one for those who have all the info needed to appear in debt regressions
gen InDebtReg =(AnyDebt~=. & EITC_v3~=. & tage~=. & AgeSq~=. & Female~=. & Married~=. & White~=. & HSGrad~=. & SomeCollege~=. & CollegeGrad~=. &  unemp_rate~=. & r_MaxWelf~=. & state_rate_wages~=. & minwage~=. & gdp~=. & any_w_a_extended~=. & rhcalyr~=. & tfipsst~=. & MissingDebtInfo==0)


** It may be useful to identify earliest observation with non-missing health info (and all the other
** info needed to be included in health regression)
gen swave_temp = swave
replace swave_temp = . if MissingHealthInfo==1 | InHealthReg~=1
sort GP_UniquePersonID
by GP_UniquePersonID: egen min_swave_Health = min(swave_temp)
drop swave_temp


** It may be useful to identify earliest observation with non-missing financial info (and all the other
** info needed to be included in financial regressions)
gen swave_temp = swave
replace swave_temp = . if MissingDebtInfo==1 | InDebtReg~=1
sort GP_UniquePersonID
by GP_UniquePersonID: egen min_swave_Debt = min(swave_temp)
drop swave_temp



gen Annualized_HH_inc = thtotinc*3
gen r_Annualized_HH_inc = real*Annualized_HH_inc




save OrganizedEmptyNesterOldGenData, replace


